package com.weng.platform.stories.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.constants.ResultCodeEnum;
import com.weng.framework.core.exception.GlobalServiceException;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.stories.entity.Note;
import com.weng.platform.stories.mapper.NoteMapper;
import com.weng.platform.stories.model.req.StudyNoteReq;
import com.weng.platform.stories.service.INoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;


/**
 * 学员学习笔记 服务实现类
 *
 * @author wenghui
 * @date 2020-08-01 14:33:53
 *
 */ 
@Slf4j
@Service
public class NoteServiceImpl extends BaseServiceImpl<NoteMapper, Note> implements INoteService {
	@Autowired
	private NoteMapper noteMapper;
	

	/**
	 * 分页查询学员学习笔记
	 * @return
	 */
	@Override
	public IPage<Note> list(Integer pageNo, Integer pageSize, StudyNoteReq studyNoteReq) {
		QueryWrapper<Note> qw = new QueryWrapper<>();
		if(StringUtils.isNotEmpty(studyNoteReq.getIterationId())){
            qw.eq("iteration_id",studyNoteReq.getIterationId());
        }
        if(StringUtils.isNotEmpty(studyNoteReq.getStoryId())){
            qw.eq("story_id",studyNoteReq.getStoryId());
        }
        if(StringUtils.isNotEmpty(studyNoteReq.getUserId())){
            qw.eq("user_id",studyNoteReq.getUserId());
        }
        qw.orderByDesc("update_time");
		IPage<Note> page = new PageModel<>(pageNo,pageSize);
		return noteMapper.selectPage(page,qw);
	}


	/**
	 * 获取单个学员学习笔记
	 * @param id
	 * @return
	 */
	@Override
	public Note getById(Serializable id) {
		return super.getById(id);
	}

	/**
	 * 新增单个学员学习笔记
	 * @param note
	 * @return
	 */
	@Override
	public Note add(Note note) throws GlobalServiceException {
		
		noteMapper.insert(note);
		return note;
	}

	/**
	 * 更新单个学员学习笔记
	 * @param note
	 * @return
	 * @throws GlobalServiceException
	 */
	@Override
	public int update(Note note) throws GlobalServiceException {
		
		return noteMapper.updateById(note);

	}


	@Override
	public ResponseData removeByIds(String ids) {
		String[] studyNoteIds = ids.split(",");
		boolean flag = this.removeByIds(Arrays.asList(studyNoteIds));
		if (flag) {
			return ResponseData.success();
		} else {
			throw new GlobalServiceException(ResultCodeEnum.DATABASE_OPERATE_EXCEPTION.getCode(), "删除学员学习笔记：" + Arrays.asList(studyNoteIds).toString() + ResultCodeEnum.DATABASE_OPERATE_EXCEPTION.getMessage());
		}
	}

}
